package com.huanying.goodjob.business;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;

@Service
public class BusinessListServiceImpl implements BusinessListService{
	@Autowired
	private BaseDao dao;

	@Override
	public PageBean searchBusiness(Map<String, String> map, int pageSize, int page) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder hql = new StringBuilder("from BusinessList as b where 1=1");
		List<Object> param = new ArrayList<Object>();
		if(map.get("name")!=null && map.get("name")!="")
		{
			hql.append(" and b.adminuser.name like ?");
			param.add("%"+map.get("name")+"%");
		}
		if(map.get("shopname")!=null && map.get("shopname")!="")
		{
			hql.append(" and b.shopname like ?");
			param.add("%"+map.get("shopname")+"%");
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
	    final int currentPage = PageBean.countCurrentPage(page);
	    List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);
	    
	    //把分页信息保存到Bean中
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
		
		return pageBean;
	}

	@Override
	public BusinessList get_byid(int id) throws Exception {
		// TODO Auto-generated method stub
		return (BusinessList)dao.load(BusinessList.class, id);
	}

	@Override
	public void update_business(BusinessList business) throws Exception {
		// TODO Auto-generated method stub
		dao.update(business);
	}

	

}
